﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Xceed.Wpf.DataGrid;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Windows.Controls.Primitives;
using System.Collections;

namespace iVision
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class CustomerDetails : Page
    {
        private ListViewFilter filters;
        private String clientID;
        private DataTable allCustomers;
        private bool isCustomerGridDataContextUpdate = false; //To keep all grid textboxes from showing pop-ups.
        private int minNumOfCharForAutoComplete = 2;
        
        public String ClientID
        {
            get { return clientID; }
            set { clientID = value; }
        }

        public CustomerDetails()
        {
            InitializeComponent();

            tbFirstName.ApplyTemplate();

            //Populate ListViews
            DataSet dataSet = new DataSet();

            string connString = Properties.Settings.Default.ConnectionString;

            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("Select ID,Amount from tblOrders",conn);
          
            adapter.Fill(dataSet);
            //dgcLenses.ItemsSource = new DataGridCollectionView(dataSet.Tables[0].DefaultView);
            gridCustomerDetails.DataContext = dataSet.Tables[0].DefaultView;
           // dgExaminations.ItemsSource = new DataGridCollectionView(dataSet.Tables[0].DefaultView);
           
            lvLatestOrders.ItemsSource = dataSet.Tables[0].DefaultView;

            //get all customers (for later filtering)
            allCustomers = App.dal.getAllCustomers();

            //Initialize ListViewFilter member.

            List<ListView> listViews = new List<ListView>();
            listViews.Add(lvLatestOrders);
            listViews.Add(lvLatestExams);

            filters = new ListViewFilter(listViews);
           
            
            
        }
        //This fires if user filters a ListView using one of the headers textboxes.
        private void ListView_TextChanged(object sender, TextChangedEventArgs e)
        {
            //Get listView
            ListView lv = (ListView)sender;
            //Get TextBox
            TextBox tb = (TextBox)e.OriginalSource;
            filters.addFilterToListViewFilterList(lv, (String)tb.Tag, tb.Text);
            filters.filterListView(lv);
     
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            if (NavigationService.CanGoBack)
            NavigationService.GoBack();
            
        }

     

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            
        }
        //This fires up if the user changes a detail in the Customer Details grid.
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
           
            if (!isCustomerGridDataContextUpdate)
            {
                if (this.IsLoaded)
                {

                    TextBox tb = (TextBox)e.OriginalSource;
                    //Ceck If text in textbox longer than minimum needed for autocomplete
                    if (tb.Text.Length > minNumOfCharForAutoComplete) 
                    {
                        BindingListCollectionView view = (BindingListCollectionView)CollectionViewSource.GetDefaultView(allCustomers);
                        DataView dv = view.SourceCollection as DataView;

                        DataTable dt = dv.Table;

                        view.CustomFilter = filters.generateSingleFilterStatement(dt, ((TextBox)e.OriginalSource).Tag.ToString(), ((TextBox)e.OriginalSource).Text);
                        if (view.Count == 0) return; //No rows to display, then do nothing.
                        //Else bind listbox and open pop-up.
                        AutoCompleteListBox.ItemsSource = null;
                        AutoCompleteListBox.ItemsSource = view;
                        AutoCompletePopUp.IsOpen = true;

                        e.Handled = true; //Event handled here.
                    }

                }
            }

        }


        //Handles the selectionchanged event from the listbox of the autocomplete feature.
        private void gridCustomerDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (AutoCompleteListBox.SelectedIndex == -1) return;
            isCustomerGridDataContextUpdate = true;
            gridCustomerDetails.DataContext = null;
            gridCustomerDetails.DataContext = AutoCompleteListBox.SelectedItem;
            
            AutoCompletePopUp.IsOpen = false;
            
            e.Handled = true;
            isCustomerGridDataContextUpdate = false;

        }

        private void btnCloseAutoCompletePopUp_Click(object sender, RoutedEventArgs e)
        {
            AutoCompletePopUp.IsOpen = false;
        }

    

       

    }
}
